package com.imageim.yimirror.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.imageim.yimirror.common.event.EventMessage;
import com.imageim.yimirror.common.util.LogUtil;
import com.imageim.yimirror.common.util.ToastUtil;
import com.imageim.yimirror.service.statistic.StatisticSDK;
import com.imageim.yimirror.service.statistic.StatisticUtil;
import com.imageim.yimirror.ui.fragment.DressGuideFragment;
import com.imageim.yimirror.ui.fragment.IntelligenRecommendFragment;
import com.imageim.yimirror.ui.fragment.IntelligentAnalysisFragment;
import com.imageim.yimirror.ui.fragment.RecommendForYouFragment;
import com.imageim.yimirror.ui.fragment.TryClothPhotoFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by zhouhanjun on 2017/12/4.
 */

public abstract class BaseFragment extends Fragment {
    private static final String TAG = "BaseFragment";

    protected Activity mContext;
    private Unbinder mUnbinder;
    protected String pageId; // 页面id

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = (Activity) context;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            // 统计时长开始
            int pageType = getPageType();
            if (pageType != 0) {
                StatisticSDK.startPage(pageId, pageType);
            }
        } else {
            int pageType = getPageType();
            if (pageType != 0) {
                StatisticSDK.endPage(pageId, pageType);
            }
        }
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutId(), container, false);
        mUnbinder = ButterKnife.bind(this, view);
        // 统计页面时长
        int pageType = getPageType();
        pageId = StatisticUtil.getPageId(getClass().getName());
        if (pageType != 0) {
            StatisticSDK.startPage(pageId, pageType);
        }
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initListener();
        initData();
    }


    private int getPageType() {
        if (this instanceof RecommendForYouFragment) {
            //为您推荐
            return 1;
        } else if (this instanceof IntelligentAnalysisFragment) {
            //智能分析
            return 2;
        } else if (this instanceof IntelligenRecommendFragment) {
            //智能推荐
            return 3;
        } else if (this instanceof DressGuideFragment) {
            //穿衣指南
            return 4;
        } else if (this instanceof TryClothPhotoFragment) {
            //试穿留影
            return 5;
        }
        return 0;
    }


    protected abstract int getLayoutId();

    protected abstract void initListener();

    protected abstract void initData();


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        //        if (BuildConfig.DEBUG) {
        //            RefWatcher refWatcher = BaseApplication.getRefWatcher(mContext);
        //            refWatcher.watch(this);
        //        }
    }

    @Subscribe
    public void onEventMainThread(EventMessage event) {

    }

    protected void toast(String msg) {
        ToastUtil.show(msg);
    }


    protected void logD(String tag, String msg) {
        LogUtil.d(tag, msg);
    }
}
